interface ITree {
    label: string,
    key: string,
    children?: ITree[]
}
export function listToTree(list: any[]) {
    const map: any = {};
    const tree: ITree[] = [];

    // 将数据转换为map,方便查找
    list.forEach(item => {
        map[item._id] = item;
        delete item.children;
    });

    // 遍历列表，将每个元素添加到其父元素的children数组中
    list.forEach(i => {
        const item = map[i._id];
        item.label = i.description;
        item.key = i._id;
        if (item.parent) {
            const parent = map[item.parent];
            if (!parent.children) {
                parent.children = [];
            }
            parent.children.push(item);
        } else {
            tree.push(item);
        }
    });
    console.log(tree)
    return tree;
}
